<template>

  <div class="login-container"  :style="{backgroundImage:'url('+require('@/assets/timg.jpg')+')'}">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login-form">
      <el-form-item label="用户名" prop="loginid">
        <el-input type="text" v-model="ruleForm.loginid" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="loginpsw">
        <el-input type="password" v-model="ruleForm.loginpsw" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>


</template>

<script>
  import  qs from 'qs'
  import Cookie from 'js-cookie'
  export default {
    data() {
      var checkName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('名字不能为空'));
        }else{
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          callback();
        }
      };

      return {
        ruleForm: {
          loginid: '',
          loginpsw: '',

        },
        //验证规则
        rules: {
          loginpsw: [
            { validator: validatePass, trigger: 'blur' }
          ],

          loginid: [
            { validator: checkName, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {


      submitForm(formName) {
        // 提交表单，登录验证
        this.$refs[formName].validate((valid) => {

          if (valid) {
            // 清除Cookie
            Cookie.remove("JSESSIONID");
            // alert(qs.stringify(this.ruleForm));
            this.$axios({
              method:'post',
              url:'t-login/login',
              data:qs.stringify(this.ruleForm)
            }).then(r=>{
              alert(JSON.stringify(r.data.data.roles))
              // 将从后台获取的用户信息存储到session中
              sessionStorage.setItem("user",JSON.stringify(r.data.data));
              sessionStorage.setItem("roles",JSON.stringify(r.data.data.roles));
              sessionStorage.setItem("menus",JSON.stringify(r.data.data.menus));
              // 登录成功，进行信息提示
              if (r.data.code == 200){
                this.$message({
                  message: r.data.msg,
                  type: 'success'
                });

                // 跳转到主页面
                this.$router.push("/homePage");
              }
            })
          } else {
            this.$message({
              message: '请完整输入账号与密码',
              type: 'warning'
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style >

  .login-form {
    width: 350px;
    margin: 160px auto; /* 上下间距160px，左右自动居中*/
    background-color: rgb(255, 255, 255,0.8); /* 透明背景色 */
    padding: 30px;
    border-radius: 20px; /* 圆角 */
  }

  /* 背景 */
  .login-container {
    position: fixed;
    width: 100%;
    height: 100%;
  }

  /* 标题 */
  .login-title {
    color: #303133;
    text-align: center;
  }

</style>

